package com.gem.day12.homework;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
	private static ArrayList<User> users = new ArrayList<User>();
	private static ArrayList<Good> goods = new ArrayList<Good>();
	private static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {
		System.out.println("**********欢迎进入超市购物系统************");
		Good good1 = new Good("西瓜", 5.4, 100);
		Good good2 = new Good("香蕉", 3.4, 100);
		Good good3 = new Good("苹果", 2.4, 100);
		Good good4 = new Good("橘子", 6.4, 100);
		Good good5 = new Good("橙子", 6.4, 100);
		goods.add(good1);
		goods.add(good2);
		goods.add(good3);
		goods.add(good4);
		goods.add(good5);
		while (true) {
			System.out.println("***********************************");
			System.out.println("1.注册");
			System.out.println("2.登录");
			System.out.println("************************************");
			System.out.println("请输入您的选择:");
			int choice = Integer.parseInt(scanner.next());
			switch (choice) {
			case 1:
				register();
				break;
			case 2:
				menu(login());
				break;
			default:
				System.out.println("您的输入不正确");
				break;
			}
		}
	}

	public static void register() {
		System.out.println("请输入用户名:");
		String unameString = scanner.next();
		System.out.println("请输入密码:");
		String pwdString = scanner.next();
		User user = new User(unameString, pwdString);
		if (users.add(user)) {
			System.out.println("注册成功");
		} else {
			System.out.println("注册失败");
		}

	}

	public static User login() {
		System.out.println("请输入用户名:");
		String unameString = scanner.next();
		System.out.println("请输入密码:");
		String pwdString = scanner.next();
		for (User user : users) {
			if (user.getUname().equals(unameString) && user.getPwd().equals(pwdString)) {
				System.out.println("登录成功");
				return user;
			}
		}
		return null;
	}

	public static void menu(User user) {
		while (true) {
			System.out.println("*************************");
			System.out.println("1.查看商品列表");
			System.out.println("2.购物");
			System.out.println("3.历史订单");
			System.out.println("4.返回上一层");
			System.out.println("*************************");
			System.out.println("请输入您的选择:");
			int choice = Integer.parseInt(scanner.next());
			switch (choice) {
			case 1:
				System.out.println("商品名\t价格\t数量");
				for (Good good : goods) {
					System.out.println(good);
				}
				break;
			case 2:
				System.out.println("请输入商品名:");
				String gname = scanner.next();
				System.out.println("请输入数量:");
				int num = Integer.parseInt(scanner.next());
				for (Good good : goods) {
					if (good.getGnameString().equals(gname)) {
						if (good.getNum() < num) {
							System.out.println("商品数量不足");
							break;
						}
						Order order = new Order(gname, good.getPrice(), num);
						if (user.getMoney() < order.getSumPrice()) {
							break;
						}
						user.setMoney(user.getMoney() - order.getSumPrice());
						user.getOrders().add(order);
						good.setNum(good.getNum() - num);
						System.out.println("购物成功");
					}
				}
				System.out.println("您输入商品名不存在");
				break;
			case 3:
				System.out.println("商品名\t价格\t数量\t总价\t时间");
				for (Order order : user.getOrders()) {
					System.out.println(order);
				}
				break;
			case 4:
				return;
			default:
				System.out.println("您的输入不正确");
				break;
			}
		}
	}

}
